package hut.ai.team.table;

import hut.ai.team.object.Chot;
import hut.ai.team.object.Ma;
import hut.ai.team.object.Phao;
import hut.ai.team.object.QuanCo;
import hut.ai.team.object.Sy;
import hut.ai.team.object.Tinh;
import hut.ai.team.object.Tuong;
import hut.ai.team.object.Xe;
import hut.ai.team.screen.MainScreen;

import java.util.ArrayList;

import com.badlogic.gdx.scenes.scene2d.actions.Actions;

public class VanCo {
	// Cac quan bi an
	public static ArrayList<QuanCo> QuanDoBiAn = new ArrayList<QuanCo>();
	public static ArrayList<QuanCo> QuanDenBiAn = new ArrayList<QuanCo>();
	// -------------------
	public static boolean resume = false;
	public static int winner = 2;
	public static boolean DangChoi = false;
	public static boolean Finish = false;
	public static int LuotDi = 1;
	public static int ply = 0;
	public static boolean Marked = false; // Kiem tra xem quan co co duoc chon hay ko
	public static QuanCo DanhDau; // Tham chieu den quan co duoc chon trong 1 nuoc di
	public static ArrayList<NuocDi> GameLog = new ArrayList<NuocDi>(); // Luu cac nuoc di
	public static int turn = 0;// Luu tong so luot di cua van co - dung cho AI
	
	public static void KhoiTaoVanCo(){
		winner = 2;
		LuotDi = 1;
		DanhDau = new QuanCo();
	}

	// --------------------- Method ChieuTuong
	public static boolean ChieuTuong(QuanCo tuong) {
		boolean chieu = false;
		int xe0 = 0, phao0 = 0, ma0 = 0, chot0 = 0, sy0 = 0, tinh0 = 0, tuong0 = 0;
		if (MainScreen.getInstance().mode == 2) { 
			if (tuong.Phe == MainScreen.getInstance().n1.Phe) {
				for (int i = 0; i < MainScreen.getInstance().n2.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().n2.dsQuanCo.get(i).TrangThai == 2) {
						if (MainScreen.getInstance().n2.dsQuanCo.get(i).Loai == 7) {
							if (Chot.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) chot0 = 1;
						}
						if (MainScreen.getInstance().n2.dsQuanCo.get(i).Loai == 6) {
							if (Ma.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) ma0 = 1;
						}
						if (MainScreen.getInstance().n2.dsQuanCo.get(i).Loai == 5) {
							if (Phao.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) phao0 = 1;
						}
						if (MainScreen.getInstance().n2.dsQuanCo.get(i).Loai == 4) {
							if (Xe.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) xe0 = 1;
						}
						if (MainScreen.getInstance().n2.dsQuanCo.get(i).Loai == 3) {
							if (Tinh.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) tinh0 = 1;
						}
						if (MainScreen.getInstance().n2.dsQuanCo.get(i).Loai == 2) {
							if (Sy.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) sy0 = 1;
						}
					}
				}
			} else {
				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().n1.dsQuanCo.get(i).TrangThai == 2) {
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 7) {
							if (Chot.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) chot0 = 1;
						}
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 6) {
							if (Ma.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) ma0 = 1;
						}
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 5) {
							if (Phao.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) phao0 = 1;
						}
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 4) {
							if (Xe.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) xe0 = 1;
						}
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 3) {
							if (Tinh.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) tinh0 = 1;
						}
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 2) {
							if (Sy.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) sy0 = 1;
						}
					}
				}
			}
		} else {
			// P vs C
			if (tuong.Phe == MainScreen.getInstance().n1.Phe) {
				for (int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().computer.dsQuanCo.get(i).TrangThai == 2) {
						if (MainScreen.getInstance().computer.dsQuanCo.get(i).Loai == 7) {
							if (Chot.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) chot0 = 1;
						}
						if (MainScreen.getInstance().computer.dsQuanCo.get(i).Loai == 6) {
							if (Ma.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) ma0 = 1;
						}
						if (MainScreen.getInstance().computer.dsQuanCo.get(i).Loai == 5) {
							if (Phao.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) phao0 = 1;
						}
						if (MainScreen.getInstance().computer.dsQuanCo.get(i).Loai == 4) {
							if (Xe.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) xe0 = 1;
						}
						if (MainScreen.getInstance().computer.dsQuanCo.get(i).Loai == 3) {
							if (Tinh.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) tinh0 = 1;
						}
						if (MainScreen.getInstance().computer.dsQuanCo.get(i).Loai == 2) {
							if (Sy.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) sy0 = 1;
						}
					}
				}
			} else {
				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().n1.dsQuanCo.get(i).TrangThai == 2) {
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 7) {
							if (Chot.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) chot0 = 1;
						}
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 6) {
							if (Ma.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) ma0 = 1;
						}
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 5) {
							if (Phao.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) phao0 = 1;
						}
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 4) {
							if (Xe.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) xe0 = 1;
						}
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 3) {
							if (Tinh.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i),	tuong.Hang, tuong.Cot) == 1) tinh0 = 1;
						}
						if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 2) {
							if (Sy.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(i), tuong.Hang, tuong.Cot) == 1) sy0 = 1;
						}
					}
				}
			}
		}
		if (xe0 != 1 && phao0 != 1 && ma0 != 1 && chot0 != 1 && sy0 != 1 && tinh0 != 1 && tuong0 != 1)
			chieu = false;
		else
			chieu = true;
		return chieu;
	}

	public static void DatQuanCo(QuanCo q, int i, int j) {
		if (q.TrangThai == 1) {
			q.TrangThai = 2;
		}
		MainScreen.getInstance().mBanCo.ViTri[i][j].Trong = false;
		MainScreen.getInstance().mBanCo.ViTri[i][j].Phe = q.Phe;
		MainScreen.getInstance().mBanCo.ViTri[i][j].TrangThai = q.TrangThai;
		if (MainScreen.getInstance().mBanCo.ViTri[i][j].TrangThai == 1)
			MainScreen.getInstance().mBanCo.ViTri[i][j].Loai = q.LoaiCL;
		else
			MainScreen.getInstance().mBanCo.ViTri[i][j].Loai = q.Loai;

		if (VanCo.DanhDau.TrangThai == 1) {
			VanCo.DanhDau.TrangThai = 2;
		}
		VanCo.DanhDau.Hang = i;
		VanCo.DanhDau.Cot = j;
		VanCo.DanhDau.addAction(Actions.moveTo(j * 50 + 18, i * 53 + 105, 1.5f));
	}

	// --------------------- Method DoiLuotDi
	public static void DoiLuotDi() {
		if (VanCo.LuotDi == 2) {
			VanCo.LuotDi = 1;
		} else {
			VanCo.LuotDi = 2;
		}
		if (MainScreen.getInstance().mode == 2) {
			if (VanCo.LuotDi == MainScreen.getInstance().n1.Phe) {
				for (int i = 0; i < MainScreen.getInstance().n2.dsQuanCo.size(); i++) {
					MainScreen.getInstance().n2.dsQuanCo.get(i).Khoa = true;
				}

				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					MainScreen.getInstance().n1.dsQuanCo.get(i).Khoa = false;
				}
			} else {
				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					MainScreen.getInstance().n1.dsQuanCo.get(i).Khoa = true;
				}

				for (int i = 0; i < MainScreen.getInstance().n2.dsQuanCo.size(); i++) {
					MainScreen.getInstance().n2.dsQuanCo.get(i).Khoa = false;
				}
			}
		} else {
			for (int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++) {
				MainScreen.getInstance().computer.dsQuanCo.get(i).Khoa = true;
			}
			if (VanCo.LuotDi == MainScreen.getInstance().n1.Phe) {
				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					MainScreen.getInstance().n1.dsQuanCo.get(i).Khoa = false;
				}
			} else {
				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					MainScreen.getInstance().n1.dsQuanCo.get(i).Khoa = true;
				}
			}
		}
	}

	// -----------------------Method AnQuanCo
	public static void AnQuanCo(QuanCo q) {
		if (MainScreen.getInstance().mode == 2) {
			if (q.Phe == MainScreen.getInstance().n2.Phe) {
				for (int i = 0; i < MainScreen.getInstance().n2.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().n2.dsQuanCo.get(i).Hang == q.Hang && MainScreen.getInstance().n2.dsQuanCo.get(i).Cot == q.Cot) {
						MainScreen.getInstance().n2.dsQuanCo.get(i).TrangThai = 0;
						MainScreen.getInstance().n2.dsQuanCo.get(i).setVisible(false);
					}
				}
			} else {
				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().n1.dsQuanCo.get(i).Hang == q.Hang && MainScreen.getInstance().n1.dsQuanCo.get(i).Cot == q.Cot) {
						MainScreen.getInstance().n1.dsQuanCo.get(i).TrangThai = 0;
						MainScreen.getInstance().n1.dsQuanCo.get(i).setVisible(false);
					}
				}
			}
		} else {
			if (q.Phe == MainScreen.getInstance().n1.Phe) {
				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().n1.dsQuanCo.get(i).Hang == q.Hang && MainScreen.getInstance().n1.dsQuanCo.get(i).Cot == q.Cot) {
						MainScreen.getInstance().n1.dsQuanCo.get(i).TrangThai = 0;
						MainScreen.getInstance().n1.dsQuanCo.get(i).setVisible(false);
					}
				}
			} else {
				for (int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().computer.dsQuanCo.get(i).Hang == q.Hang && MainScreen.getInstance().computer.dsQuanCo.get(i).Cot == q.Cot) {
						MainScreen.getInstance().computer.dsQuanCo.get(i).TrangThai = 0;
						MainScreen.getInstance().computer.dsQuanCo.get(i).setVisible(false);
					}
				}
			}
		}
	}

	public static void HoiSinhQuanCo(QuanCo q) {
		if (MainScreen.getInstance().mode == 2) {
			if (q.Phe == MainScreen.getInstance().n2.Phe) {
				for (int i = 0; i < MainScreen.getInstance().n2.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().n2.dsQuanCo.get(i).Hang == q.Hang && MainScreen.getInstance().n2.dsQuanCo.get(i).Cot == q.Cot) {
						MainScreen.getInstance().n2.dsQuanCo.get(i).TrangThai = q.TrangThai;
						MainScreen.getInstance().n2.dsQuanCo.get(i).setVisible(true);
					}
				}
			} else {
				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().n1.dsQuanCo.get(i).Hang == q.Hang && MainScreen.getInstance().n1.dsQuanCo.get(i).Cot == q.Cot) {
						MainScreen.getInstance().n1.dsQuanCo.get(i).TrangThai = q.TrangThai;
						MainScreen.getInstance().n1.dsQuanCo.get(i).setVisible(true);
					}
				}
			}
		} else {
			if (q.Phe == MainScreen.getInstance().n1.Phe) {
				for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().n1.dsQuanCo.get(i).Hang == q.Hang && MainScreen.getInstance().n1.dsQuanCo.get(i).Cot == q.Cot) {
						MainScreen.getInstance().n1.dsQuanCo.get(i).TrangThai = q.TrangThai;
						MainScreen.getInstance().n1.dsQuanCo.get(i).setVisible(true);
					}
				}
			} else {
				for (int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++) {
					if (MainScreen.getInstance().computer.dsQuanCo.get(i).Hang == q.Hang && MainScreen.getInstance().computer.dsQuanCo.get(i).Cot == q.Cot) {
						MainScreen.getInstance().computer.dsQuanCo.get(i).TrangThai = q.TrangThai;
						MainScreen.getInstance().computer.dsQuanCo.get(i).setVisible(true);
					}
				}
			}
		}
	}

	public static void KiemTraChieuBi() {
		boolean cuu = false;
		int tuong = 0, sy0 = 0, tinh0 = 0,xe0 = 0, phao0 = 0, ma0 = 0, chot0 = 0;
		if (MainScreen.getInstance().mode == 2) // che do choi PvsP
		{
			if (VanCo.LuotDi == MainScreen.getInstance().n1.Phe) {
				if (ChieuTuong(MainScreen.getInstance().n1.dsQuanCo.get(0)) == true) {
					for (int i = 0; i <= 9; i++) {
						for (int j = 0; j <= 8; j++) {
							for (int k = 0; k < MainScreen.getInstance().n1.dsQuanCo.size(); k++) {
								if (MainScreen.getInstance().n1.dsQuanCo.get(k).TrangThai == 1) { // quan co up
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 2){
										if (Sy.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Sy.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 3){
										if (Tinh.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Tinh.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) tinh0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 4){
										if (Xe.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Xe.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) xe0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 5){
										if (Phao.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Phao.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) phao0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 6){
										if (Ma.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Ma.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) ma0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 7){
										if (Chot.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Chot.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
								}else if(MainScreen.getInstance().n1.dsQuanCo.get(k).TrangThai == 2){
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 1){
										if (Tuong.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Tuong.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) tuong = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 2){
										if (Sy.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Sy.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 3){
										if (Tinh.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Tinh.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) tinh0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 4){
										if (Xe.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Xe.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) xe0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 5){
										if (Phao.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Phao.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) phao0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 6){
										if (Ma.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Ma.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) ma0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 7){
										if (Chot.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Chot.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
								}
							}
							if ((tuong == 1) || (sy0 == 1) || (tinh0 == 1) 
									|| (xe0 == 1) || (phao0 == 1) || (ma0 == 1) 
									|| (chot0 == 1)) {
								cuu = true;
								break;
							}
						}
					}
				} else
					cuu = true;
				if (!cuu)
					winner = 1;
			} else {
				if (ChieuTuong(MainScreen.getInstance().n2.dsQuanCo.get(0)) == true) {
					for (int i = 0; i <= 9; i++) {
						for (int j = 0; j <= 8; j++) {
							for (int k = 0; k < MainScreen.getInstance().n2.dsQuanCo.size(); k++) {
								if (MainScreen.getInstance().n2.dsQuanCo.get(k).TrangThai == 1) { // quan co up
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).LoaiCL == 2){
										if (Sy.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Sy.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).LoaiCL == 3){
										if (Tinh.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Tinh.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) tinh0 = 1;
									}
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).LoaiCL == 4){
										if (Xe.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Xe.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) xe0 = 1;
									}
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).LoaiCL == 5){
										if (Phao.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Phao.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) phao0 = 1;
									}
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).LoaiCL == 6){
										if (Ma.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Ma.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) ma0 = 1;
									}
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).LoaiCL == 7){
										if (Chot.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Chot.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
								}else if(MainScreen.getInstance().n2.dsQuanCo.get(k).TrangThai == 2){
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).Loai == 1){
										if (Tuong.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Tuong.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) tuong = 1;
									}
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).Loai == 2){
										if (Sy.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Sy.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).Loai == 3){
										if (Tinh.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Tinh.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) tinh0 = 1;
									}
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).Loai == 4){
										if (Xe.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Xe.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) xe0 = 1;
									}
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).Loai == 5){
										if (Phao.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Phao.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) phao0 = 1;
									}
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).Loai == 6){
										if (Ma.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Ma.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) ma0 = 1;
									}
									if(MainScreen.getInstance().n2.dsQuanCo.get(k).Loai == 7){
										if (Chot.KiemTra(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1	&& Chot.TuongAnToan(MainScreen.getInstance().n2.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
								}
							}
							if ((tuong == 1) || (sy0 == 1) || (tinh0 == 1) 
									|| (xe0 == 1) || (phao0 == 1) || (ma0 == 1) 
									|| (chot0 == 1)) {
								cuu = true;
								break;
							}
						}
					}
				} else
					cuu = true;
				if (!cuu)
					winner = 1;
			}
		}else{// che do P vs C
			if (VanCo.LuotDi == MainScreen.getInstance().n1.Phe) {
				if (ChieuTuong(MainScreen.getInstance().n1.dsQuanCo.get(0)) == true) {
					for (int i = 0; i <= 9; i++) {
						for (int j = 0; j <= 8; j++) {
							for (int k = 0; k < MainScreen.getInstance().n1.dsQuanCo.size(); k++) {
								if (MainScreen.getInstance().n1.dsQuanCo.get(k).TrangThai == 1) { // quan co up
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 2){
										if (Sy.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Sy.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 3){
										if (Tinh.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Tinh.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) tinh0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 4){
										if (Xe.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Xe.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) xe0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 5){
										if (Phao.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Phao.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) phao0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 6){
										if (Ma.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Ma.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) ma0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).LoaiCL == 7){
										if (Chot.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Chot.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
								}else if(MainScreen.getInstance().n1.dsQuanCo.get(k).TrangThai == 2){
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 1){
										if (Tuong.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Tuong.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) tuong = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 2){
										if (Sy.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Sy.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 3){
										if (Tinh.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Tinh.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) tinh0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 4){
										if (Xe.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Xe.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) xe0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 5){
										if (Phao.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Phao.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) phao0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 6){
										if (Ma.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Ma.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) ma0 = 1;
									}
									if(MainScreen.getInstance().n1.dsQuanCo.get(k).Loai == 7){
										if (Chot.KiemTra(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1	&& Chot.TuongAnToan(MainScreen.getInstance().n1.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
								}
							}
							if ((tuong == 1) || (sy0 == 1) || (tinh0 == 1) 
									|| (xe0 == 1) || (phao0 == 1) || (ma0 == 1) 
									|| (chot0 == 1)) {
								cuu = true;
								break;
							}
						}
					}
				} else
					cuu = true;
				if (!cuu)
					winner = 1;
			} else {
				if (ChieuTuong(MainScreen.getInstance().computer.dsQuanCo.get(0)) == true) {
					for (int i = 0; i <= 9; i++) {
						for (int j = 0; j <= 8; j++) {
							for (int k = 0; k < MainScreen.getInstance().computer.dsQuanCo.size(); k++) {
								if (MainScreen.getInstance().computer.dsQuanCo.get(k).TrangThai == 1) { // quan co up
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).LoaiCL == 2){
										if (Sy.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Sy.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).LoaiCL == 3){
										if (Tinh.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Tinh.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) tinh0 = 1;
									}
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).LoaiCL == 4){
										if (Xe.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Xe.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) xe0 = 1;
									}
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).LoaiCL == 5){
										if (Phao.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Phao.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) phao0 = 1;
									}
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).LoaiCL == 6){
										if (Ma.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Ma.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) ma0 = 1;
									}
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).LoaiCL == 7){
										if (Chot.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Chot.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
								}else if(MainScreen.getInstance().computer.dsQuanCo.get(k).TrangThai == 2){
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).Loai == 1){
										if (Tuong.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Tuong.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) tuong = 1;
									}
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).Loai == 2){
										if (Sy.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Sy.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).Loai == 3){
										if (Tinh.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Tinh.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) tinh0 = 1;
									}
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).Loai == 4){
										if (Xe.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Xe.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) xe0 = 1;
									}
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).Loai == 5){
										if (Phao.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Phao.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) phao0 = 1;
									}
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).Loai == 6){
										if (Ma.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Ma.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) ma0 = 1;
									}
									if(MainScreen.getInstance().computer.dsQuanCo.get(k).Loai == 7){
										if (Chot.KiemTra(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1	&& Chot.TuongAnToan(MainScreen.getInstance().computer.dsQuanCo.get(k), i, j) == 1) sy0 = 1;
									}
								}
							}
							if ((tuong == 1) || (sy0 == 1) || (tinh0 == 1) 
									|| (xe0 == 1) || (phao0 == 1) || (ma0 == 1) 
									|| (chot0 == 1)) {
								cuu = true;
								break;
							}
						}
					}
				} else
					cuu = true;
				if (!cuu)
					winner = 1;
			}
		}
	}

	// --------------------- Method KiemTraChieuTuong
	public static void KiemTraChieuTuong() {
		int t = 0;
		if (MainScreen.getInstance().mode == 2) {
			if (VanCo.ChieuTuong(MainScreen.getInstance().n1.dsQuanCo.get(0)) == true	&& VanCo.ChieuTuong(MainScreen.getInstance().n2.dsQuanCo.get(0)) == false) t = 0;
			if (VanCo.ChieuTuong(MainScreen.getInstance().n1.dsQuanCo.get(0)) == false && VanCo.ChieuTuong(MainScreen.getInstance().n2.dsQuanCo.get(0)) == true) t = 1;
			if (VanCo.ChieuTuong(MainScreen.getInstance().n1.dsQuanCo.get(0)) == false && VanCo.ChieuTuong(MainScreen.getInstance().n2.dsQuanCo.get(0)) == false) t = 2;
		} else {
			if (VanCo.ChieuTuong(MainScreen.getInstance().n1.dsQuanCo.get(0)) == true	&& VanCo.ChieuTuong(MainScreen.getInstance().computer.dsQuanCo.get(0)) == false) t = 0;
			if (VanCo.ChieuTuong(MainScreen.getInstance().n1.dsQuanCo.get(0)) == false && VanCo.ChieuTuong(MainScreen.getInstance().computer.dsQuanCo.get(0)) == true) t = 1;
			if (VanCo.ChieuTuong(MainScreen.getInstance().n1.dsQuanCo.get(0)) == false && VanCo.ChieuTuong(MainScreen.getInstance().computer.dsQuanCo.get(0)) == false) t = 2;
		}
		switch (t) {
		case 0:
			MainScreen.getInstance().n1.dsQuanCo.get(0).biChieu = true;
			if (VanCo.winner == 2) {
				// Toast.makeText(GameActivity.gameContext, "Check", Toast.LENGTH_SHORT).show();
			}
			break;
		case 1:
			if (MainScreen.getInstance().mode == 2) {
				MainScreen.getInstance().n2.dsQuanCo.get(0).biChieu = true;
			} else {
				MainScreen.getInstance().computer.dsQuanCo.get(0).biChieu = true;
			}
			if (VanCo.winner == 2) {
				// Toast.makeText(GameActivity.gameContext, "Check", Toast.LENGTH_SHORT).show();
			}
			break;
		case 2:
			if (MainScreen.getInstance().mode == 2) {
				MainScreen.getInstance().n1.dsQuanCo.get(0).biChieu = false;
				MainScreen.getInstance().n2.dsQuanCo.get(0).biChieu = false;
			} else {
				MainScreen.getInstance().n1.dsQuanCo.get(0).biChieu = false;
				MainScreen.getInstance().computer.dsQuanCo.get(0).biChieu = false;
			}
			break;
		}
	}

}
